package com.xagcdx.group1.web;


import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.xagcdx.group1.bean.Role;
import com.xagcdx.group1.bean.TbUserRole;
import com.xagcdx.group1.bean.User;
import com.xagcdx.group1.service.RoleService;
import com.xagcdx.group1.service.TbUserRoleService;
import com.xagcdx.group1.service.UserService;

/**
 * <p>
 *  鍓嶇鎺у埗鍣�
 * </p>
 *
 * @author grou1
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private TbUserRoleService tbrole;
	
	@RequestMapping("/login")
	public String login(String account, String password) {

		Subject subject = SecurityUtils.getSubject();
		if (!subject.isAuthenticated()) {
			// 绗竴姝�:灏嗙櫥褰曠殑鐢ㄦ埛鍚嶇О鍜岃处鍙锋斁鍒皊hiro鎻愪緵鐨勪护鐗岀被涓�
			UsernamePasswordToken token = new UsernamePasswordToken(account, password);
			// 绗洓姝ワ細杩涜鐧诲綍澶辫触鏃剁殑寮傚父澶勭悊
			try {
				subject.login(token);// 璋冪敤璇ョ櫥褰曟柟娉曪紝璇ユ柟娉曚細鑷姩鍘绘壘鐢ㄦ埛瀹氫箟鐨�
				
			} catch (UnknownAccountException uae) {
				return "0";
			} catch (IncorrectCredentialsException ice) {
				return "-1";
			}
		}
		return "1";
	}
	
	
	@RequestMapping("/userListPage")
	public String userListPage(String uname, Integer limit, Integer page, String rid) {
		Map<String, Object> result = new HashMap<String, Object>(10);

		result.put("count", userService.counts(uname, rid));
		result.put("msg", "");
		result.put("code", 0);
		result.put("data", userService.userlistPage((page - 1) * limit, limit, uname, rid));

		return new Gson().toJson(result);
	}
	

	@RequestMapping("/removeUserInfo")
	public String removeUserInfo(String uid) {
		QueryWrapper<TbUserRole> queryWrapper=new QueryWrapper<TbUserRole>();
		queryWrapper.eq("uid", uid);
		
		
		boolean remove = tbrole.remove(queryWrapper);
		return new Gson().toJson(userService.removeById(uid));
	}

	@RequestMapping("/updateUserInfo")
	public String updateUserInfo(String uid, String uname, String account, String password,String rid) {
		
			
			User user=new User();
			user.setUname(uname);
			user.setAccount(account);
			user.setPassword(password);
			user.setUid(uid);
			QueryWrapper<TbUserRole> queryWrapper=new QueryWrapper<TbUserRole>();
			queryWrapper.eq("uid", uid);
			
			
			boolean remove = tbrole.remove(queryWrapper);
			if(remove) {
				boolean updateById = userService.updateById(user);
				if(updateById) {
					TbUserRole userRole=new TbUserRole();
					userRole.setRid(rid);
					userRole.setUid(user.getUid());
					tbrole.save(userRole);
					return "1";
				}else {
					return "2";
				}
				
			}else {
				return "3";
			}
		
	}

	@RequestMapping("/addUser")
	public String addUser(String rid, String uname, String account,String password) {
		
		User user=new User();
		user.setUname(uname);
		user.setAccount(account);
		user.setPassword(password);
		
		boolean save = userService.save(user);
		if(save) {
			QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
			queryWrapper.eq("account", account);
			User one = userService.getOne(queryWrapper);
			TbUserRole userRole=new TbUserRole();
			userRole.setRid(rid);
			userRole.setUid(user.getUid());
			
			boolean save2 = tbrole.save(userRole);
			if(save2) {
				return "1";
			}else {
				return "2";
			}
		}else {
			return "3";
		}
	
				
		
	}

	@RequestMapping("/chickuser")
	public String chickuser(String account) {

		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("account", account);
		User one = userService.getOne(queryWrapper);
		if (one == null) {
			return "0";
		} else {
			return "1";
		}
        
	}


	
	@RequestMapping("/changePassword")
	public String changeAccount(String password){
		 Subject subject= SecurityUtils.getSubject();
	     Session session=subject.getSession();
	     //灏嗘煡璇㈠嚭鐨勭敤鎴峰璞℃斁鍒皊ession瀵硅薄涓�
	     User user=(User)session.getAttribute("user");
	    user.setPassword(password);
	    if(userService.saveOrUpdate(user)){
	    	return "1";
	    }else {
	    	return "0";
	    }
		
	}
	
	@RequestMapping("/getRole")
	public String getRole() {
		return new Gson().toJson(roleService.list(null));
	}
	@RequestMapping("/getrolefuid")
	public String getrolefuid(String uid) {
		QueryWrapper<TbUserRole> queryWrapper = new QueryWrapper<TbUserRole>();
		queryWrapper.eq("uid", uid);
		TbUserRole tbUserRole = tbrole.getOne(queryWrapper);
		
		return tbUserRole.getRid();
	}
}

